package com.cw.jeeyt.controller.system;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cw.jeeyt.common.Constants;
import com.cw.jeeyt.common.enums.EnumYn;
import com.cw.jeeyt.service.bo.security.SecurityUserBo;
import com.cw.jeeyt.service.bo.system.SystemNoticeBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.api.dto.response.JsonResultDto;
import com.cw.jeeyt.service.directive.FooterDirective;
import com.cw.jeeyt.service.impl.cache.ThemeCache;
import com.cw.jeeyt.service.intf.security.SecurityResourceService;
import com.cw.jeeyt.service.intf.system.SystemNoticeService;

@Controller
@RequestMapping("")
public class IndexController {
    @Resource
    private ThemeCache themeCache;
    @Resource
    private SystemNoticeService systemNoticeService;
    @Resource
    private SecurityResourceService securityResourceService;

    /**
     * 跳转到系统后台用户管理首页
     *
     * @param request
     * @return
     */
    @RequestMapping("/")
    public ModelAndView toIndex(HttpServletRequest request) throws Exception {
        ModelAndView mv = new ModelAndView("/system/index");
        SecurityUserBo user = null;
        try {
            user = SessionUtil.getSessionUser();
        } catch (Exception e) {
            //e.printStackTrace();
        }
        if (user == null) {
            mv = new ModelAndView("system/system_login");
        }
        String theme = themeCache.getTheme(user.getId());
        if (StringUtils.isEmpty(theme)) {
            theme = Constants.SYSTEM_THEME_DARK;
        }
        //加载公告通知页面
        List<SystemNoticeBo> systemNoticeList = systemNoticeService.querySystemNoticeList(EnumYn.Y.getCode());
        StringBuilder sbTitle = new StringBuilder("");
        if (systemNoticeList != null && !systemNoticeList.isEmpty()) {
            for (SystemNoticeBo systemNotice : systemNoticeList) {
                sbTitle.append("<a href='view_notice?id=" + systemNotice.getId() + "&operation=view' target='_blank'>"
                    + systemNotice.getNoticeTitle() + "</a><br/>");
            }
        }
        //初始化菜单
        String menuInfo = initMenu(request);
        mv.addObject("noticeContent", sbTitle.toString());
        mv.addObject("menuInfo", menuInfo);
        mv.addObject("theme", theme);
        mv.addObject("footer", new FooterDirective());
        mv.addObject("userInfo", user);
        request.setAttribute("userName", user.getAccount());
        return mv;
    }

    private String initMenu(HttpServletRequest request) throws Exception {
        String menuInfo;
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        try {
            Long userId = userVo.getId();
            menuInfo = securityResourceService.queryResourceByLoginUser(userId, request);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
        return menuInfo;
    }

    @PostMapping("/exchangeTheme")
    @ResponseBody
    public JsonResultDto exchangeTheme() {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            SecurityUserBo user = SessionUtil.getSessionUser();
            if (user == null) {
                jsonResultDto.setMsg("用户未登录");
            } else {
                Long userId = user.getId();
                String theme = themeCache.getTheme(userId);
                if (Constants.SYSTEM_THEME_DARK.equals(theme)) {
                    themeCache.putTheme(userId, Constants.SYSTEM_THEME_WHITE);
                } else {
                    themeCache.putTheme(userId, Constants.SYSTEM_THEME_DARK);
                }
                jsonResultDto.setSuccess(true);
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResultDto.setMsg("切换主题失败：" + e.getMessage());
        }
        return jsonResultDto;
    }
}
